package org.wgc.study.job;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.wgc.study.reader.BatchSimpleItemReader;

import java.util.Arrays;
import java.util.List;
/***
 *@title BatchSimpleItemReaderJob
 *@description  测试代码BatchSimpleItemReaderJob，使用chunk()方法。chunk字面上的意思是“块”的意思，可以简单理解为数据块，泛型<String, String>用于指定读取的数据和输出的数据类型，构造器入参指定了数据块的大小，比如指定为2时表示每当读取2组数据后做一次数据输出处理。
 * 接着reader()方法指定读取数据的方式，该方法接收ItemReader的实现类。
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-08-04 15:30
 **/
@Component
public class BatchSimpleItemReaderJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    @Bean
    public Job simpleItemReaderJob() {
        return jobBuilderFactory
                .get("simpleItemReaderJob")
                .start(simpleItemReaderStep())
                .build();
    }

    public Step simpleItemReaderStep() {
        return stepBuilderFactory
                .get("simpleItemReaderStep")
                .<String, String>chunk(2)
                .reader(simpleItemReader())
                .writer(list -> list.forEach(System.out::println))
                .build();
    }

    public ItemReader<String> simpleItemReader() {
        List<String> strings = Arrays.asList("存款", "余额", "资金","冻结");
        return new BatchSimpleItemReader(strings);
    }
}
